package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author chopper
 * 同步部门信息
 *
 */
public class SyncHrmDepartmentInfo implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(SyncHrmDepartmentInfo.class);
	private String charset = "utf-8";
	private int timeout = 100000;
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		PropertiesUtil properties = new PropertiesUtil();
		def hrlurl = properties.readValue("Hrmurl");
		log.debug("Hrmurl:"+hrlurl);

		def hrlip = properties.readValue("Hrmip");
		log.debug("Hrmip:"+hrlip);

		def guid=Util.getGuid();
		log.debug("guid:"+guid);
		
		def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
							<SOAP-ENV:Body>
								<m:getHrmDepartmentInfo xmlns:m="http://localhost/services/HrmService">
									<m:in0>"""+hrlip+"""</m:in0>
									<m:in1></m:in1>
								</m:getHrmDepartmentInfo>
							</SOAP-ENV:Body>
						</SOAP-ENV:Envelope>""";
		//log.debug("调用部门信息接口发送报文信息："+xmlbody);
		//调用部门信息接口
		def xml=Util.callWebService(hrlurl,xmlbody,charset,'',timeout);
		xml = Util.Transform(xml); //去命令空间
		xml = new XmlParser().parseText(xml); //xml解析
		//log.debug("调用部门信息接口返回报文信息："+xml);
		def result ="";
		try{
			result =xml.Body.getHrmDepartmentInfoResponse.out.DepartmentBean;
			}catch (Exception e){
				result="";
				log.error("EM:",e);
			};
			log.debug("result："+result);
			log.debug("size"+result.size());
			if(result==null||(result!=null&&result.size()==0)){
				return "{\"code\":\"-1\",\"msg\":\"调用接口返回异常，请查看日志\"}";
			}
		
		result.each{
			try{
				//log.debug("Deal hrm_group_bm--" + "subcompanyid:"+it._subcompanyid.text()+" departmentid:"+it._departmentid.text()+" fullname:"+it._fullname.text()+" //shortname:"+it._shortname.text()+" showorder:"+it._showorder.text()+" //supsubcompanyid:"+it._supdepartmentid.text());
				
				//增加不存在的部门
				eliteBean.update("INSERT INTO hrm_group (rec_guid,subcompanyid,departmentid,fullname,shortname,showorder,supsubcompanyid,hrm_id,modifydate,type) SELECT newid(),bm.subcompanyid,bm.departmentid,bm.fullname,bm.shortname,bm.showorder,bm.supsubcompanyid,'"+guid+"' ,getdate(),'BM' FROM (SELECT 'FM' + '"+it._subcompanyid.text()+"' subcompanyid,'BM' + '"+it._departmentid.text()+"'departmentid,'"+it._fullname.text()+"' fullname,'"+it._shortname.text()+"' shortname,'"+it._showorder.text()+"' showorder,'FM' + '"+it._supdepartmentid.text()+"' supsubcompanyid) bm LEFT JOIN hrm_group hg ON hg.departmentid = bm.departmentid AND hg.type = 'BM' WHERE hg.departmentid IS NULL","dataSource");
				
				//更新存在的部门
				eliteBean.update("update hrm_group set subcompanyid ='FM' + '"+it._subcompanyid.text()+"',fullname ='"+it._fullname.text()+"',shortname ='"+it._shortname.text()+"',showorder ='"+it._showorder.text()+"',supsubcompanyid = 'FM' + '"+it._supdepartmentid.text()+"',hrm_id ='"+guid+"',modifydate=getdate() where type ='bm' and departmentid ='BM' + '"+it._departmentid.text()+"'","dataSource");
			}catch (Exception e){
				log.error("EM:",e);
			};
		};
		eliteBean.update("delete from hrm_group where type='BM' and hrm_id!='"+guid+"'","dataSource"); //删除部门历史数据,保留本次同步信息
		
		def sqltype = eliteBean.chaxun_sql("select count(*) from hrm_group where type='BM' and hrm_id='"+guid+"'","dataSource"); //判断部门数据是否正常插入
		if(sqltype==0) {
				return "{\"code\":\"-2\",\"msg\":\"调用部门信息接口返回为空，请查看日志\"}";
			};
		
		//删除接口同步，并且CRM无修改过的数据
		eliteBean.update("delete from org_grouplist where datafrom=1 and modifytype=0 and group_type=2","dataSource");
		
		//写入新增数据
		eliteBean.update("insert into org_grouplist (group_id,group_desc,group_type,parentgroup_id,showorder,showflag,modifydate,datafrom,modifytype) select hg.departmentid,hg.fullname,2,hg.subcompanyid,hg.showorder,1,getdate(),1,0 from hrm_group hg left join org_grouplist og on hg.departmentid=og.group_id where hg.type='BM' and og.group_id is null","dataSource");
		
		//更新接同步并且CRM已经修改过的数据
		eliteBean.update("update org_grouplist set group_desc=hg.fullname,showorder=hg.showorder from org_grouplist og,hrm_group hg where hg.departmentid=og.group_id and og.datafrom=1 and og.modifytype=1 and og.group_type=2 and hg.type='BM'","dataSource");
		return "{\"code\":\"1\",\"msg\":\"执行同步更新成功\"}";
	}
}

